//
//  VCOfClickHistory.m
//  SeeTheWorld
//
//  Created by dllo on 16/3/2.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "VCOfClickHistory.h"
#import "PCH.pch"
#import "ItemOfMuDiDi.h"
#import "ItemOfTiezi.h"
#import "ItemOfZheKou.h"
#import "ItemOfYongHuHu.h"

#import <AFNetworking.h>
#import <UIImageView+WebCache.h>


@interface VCOfClickHistory () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UIButton *buttonOfLeft;
@property (nonatomic, retain)UIView *viewForRedLine;
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)UIView *viewOfLan;

@property (nonatomic, retain)UIButton *button1;
@property (nonatomic, retain)UIButton *button2;
@property (nonatomic, retain)UIButton *button3;
@property (nonatomic, retain)UIButton *button4;

@end

@implementation VCOfClickHistory

- (void)dealloc {
    self.collectionView.delegate = nil;
    self.collectionView.dataSource = nil;
    [_button1 release];
    [_button2 release];
    [_button3 release];
    [_button4 release];
    [_text release];
    [_viewOfLan release];
    [_collectionView release];
    [_viewForRedLine release];
    [_buttonOfLeft release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationItem.title = self.text;
    
    /** 数据 */
    [self handleData];
    
    /** 主页navigation左侧button */
    self.buttonOfLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfLeft.frame = CGRectMake(5, 10, 30, 30);
    [self.buttonOfLeft addTarget:self action:@selector(fanHui:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOfLeft setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-2"] forState:UIControlStateNormal];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithCustomView:self.buttonOfLeft];
    self.navigationItem.leftBarButtonItem = bar;
    [bar release];
    
    /** 分栏 */
    self.viewOfLan = [[UIView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, 40)];
    [self.view addSubview:self.viewOfLan];
    [_viewOfLan release];
    
    self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.viewOfLan addSubview:self.button1];
    self.button1.frame = CGRectMake(0, 0, WIDTH / 4, 40);
    [self.button1 setTitle:@"目的地" forState:UIControlStateNormal];
    [self.button1 dk_setTitleColorPicker:DKColorWithColors([UIColor blackColor], [UIColor whiteColor]) forState:UIControlStateNormal];
    [self.button1 addTarget:self action:@selector(tiao1:) forControlEvents:UIControlEventTouchUpInside];

    self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.viewOfLan addSubview:self.button2];
    self.button2.frame = CGRectMake(WIDTH / 4, 0, WIDTH / 4, 40);
    [self.button2 setTitle:@"帖子" forState:UIControlStateNormal];
    [self.button2 dk_setTitleColorPicker:DKColorWithColors([UIColor blackColor], [UIColor whiteColor]) forState:UIControlStateNormal];
    [self.button2 addTarget:self action:@selector(tiao2:) forControlEvents:UIControlEventTouchUpInside];
    
    self.button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.viewOfLan addSubview:self.button3];
    self.button3.frame = CGRectMake(WIDTH / 4 * 2, 0, WIDTH / 4, 40);
    [self.button3 setTitle:@"折扣" forState:UIControlStateNormal];
    [self.button3 dk_setTitleColorPicker:DKColorWithColors([UIColor blackColor], [UIColor whiteColor]) forState:UIControlStateNormal];
    [self.button3 addTarget:self action:@selector(tiao3:) forControlEvents:UIControlEventTouchUpInside];

    self.button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.viewOfLan addSubview:self.button4];
    self.button4.frame = CGRectMake(WIDTH / 4 * 3, 0, WIDTH / 4, 40);
    [self.button4 setTitle:@"用户" forState:UIControlStateNormal];
    [self.button4 dk_setTitleColorPicker:DKColorWithColors([UIColor blackColor], [UIColor whiteColor]) forState:UIControlStateNormal];
    [self.button4 addTarget:self action:@selector(tiao4:) forControlEvents:UIControlEventTouchUpInside];


#pragma mark - 创建横线
    self.viewForRedLine = [[UIView alloc] initWithFrame:CGRectMake(0, 64 + 40, WIDTH / 4, 2)];
    self.viewForRedLine.backgroundColor = [UIColor colorWithRed:43 / 256.f green:115 / 256.f blue:217 / 256.f alpha:1];
    [self.view addSubview:self.viewForRedLine];
    
#pragma mark - 创建collectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT - 64 - 2 - 40);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 + 40 + 2, WIDTH, HEIGHT - 64 - 2 - 40) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[ItemOfMuDiDi class] forCellWithReuseIdentifier:@"pool1"];
    [self.collectionView registerClass:[ItemOfTiezi class] forCellWithReuseIdentifier:@"pool2"];
    [self.collectionView registerClass:[ItemOfZheKou class] forCellWithReuseIdentifier:@"pool3"];
    [self.collectionView registerClass:[ItemOfYongHuHu class] forCellWithReuseIdentifier:@"pool4"];
    
    /** KVO监听 */
    [self createKVOAction];
    
}
- (void)tiao1:(UIButton *)button {
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}


- (void)tiao2:(UIButton *)button {
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

- (void)tiao3:(UIButton *)button {
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:2 inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

- (void)tiao4:(UIButton *)button {
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:3 inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

#pragma mark - 数据
- (void)handleData {
    
    /** 汉字转码 */
    self.text =[self.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}

/** 返回cell */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.item == 0) {
        ItemOfMuDiDi *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
        cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        [cell passText:self.text];
        return cell;
        
    } else if (indexPath.item == 1) {
        ItemOfTiezi *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool2" forIndexPath:indexPath];
        cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        [cell passText:self.text];
        return cell;
        
    } else if (indexPath.item == 2) {
        ItemOfZheKou *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool3" forIndexPath:indexPath];
        [cell passText:self.text];
        cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        return cell;
        
    } else {
        ItemOfYongHuHu *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool4" forIndexPath:indexPath];
        cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        [cell passText:self.text];
        return cell;
    }
    
  
}

/** 返回item个数 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 4;
 
}

#pragma mark - 核心: 观察者 观察某一个对象的属性变化
- (void)createKVOAction {
    
    /** VC观察collection的contentOffset. */
    [self.collectionView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:@"context"];
    
}

#pragma mark - 当被观察者属性发生变化的时候, 调用此方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    CGFloat x = [[change objectForKey:@"new"] CGPointValue].x;
    
    CGFloat redX = x / 4;
    
    /** 修改RedLine位置 */
    self.viewForRedLine.transform = CGAffineTransformMakeTranslation(redX, 0);
    
    /** 修改button颜色 */
    if (x == 0) {
        [self.button1 setTitleColor:[UIColor colorWithRed:43 / 256.f green:115 / 256.f blue:217 / 256.f alpha:1] forState:UIControlStateNormal];
    } else {
        [self.button1 dk_setTitleColorPicker:DKColorWithColors([UIColor blackColor], [UIColor whiteColor]) forState:UIControlStateNormal];
    }
    
    if (x == WIDTH) {
        [self.button2 setTitleColor:[UIColor colorWithRed:43 / 256.f green:115 / 256.f blue:217 / 256.f alpha:1] forState:UIControlStateNormal];
    } else {
        [self.button2 dk_setTitleColorPicker:DKColorWithColors([UIColor blackColor], [UIColor whiteColor]) forState:UIControlStateNormal];
    }
    
    if (x == WIDTH * 2) {
        [self.button3 setTitleColor:[UIColor colorWithRed:43 / 256.f green:115 / 256.f blue:217 / 256.f alpha:1] forState:UIControlStateNormal];
    } else {
        [self.button3 dk_setTitleColorPicker:DKColorWithColors([UIColor blackColor], [UIColor whiteColor]) forState:UIControlStateNormal];

    }
    
    if (x == WIDTH * 3) {
        [self.button4 setTitleColor:[UIColor colorWithRed:43 / 256.f green:115 / 256.f blue:217 / 256.f alpha:1] forState:UIControlStateNormal];
    } else {
        [self.button4 dk_setTitleColorPicker:DKColorWithColors([UIColor blackColor], [UIColor whiteColor]) forState:UIControlStateNormal];
    }
}

#pragma mark - 左上角返回按钮
- (void)fanHui:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
